package com.iamteer.contact.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iamteer.contact.dao.impl.ContactDaoImpl;
import com.iamteer.contact.entity.Contact;

/**
 * Servlet implementation class ListContact
 */
@WebServlet("/ListContact")
public class ListContact extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ListContact() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		String path = getServletContext().getRealPath("contacts.xml");///WEB-INF/classes/
//		File file = new File(path);
//		ContactDaoImpl dao = new ContactDaoImpl(file);
		ContactDaoImpl dao = new ContactDaoImpl();
		List<Contact> list = dao.findAll();
		
		response.setContentType("text/html;charset=utf-8");
		String html = "";
		html += "<!DOCTYPE html>";
		html += "<html>";
		html += "	<head>";
		html += "		<meta charset='UTF-8'>";
		html += "		<title>所有联系人</title>";
		html += "		<style type='text/css'>";
		html += "			/*文字居中*/";
		html += "			table td{";
		html += "				text-align: center;";
		html += "			}";
		html += "			/*合并表格边框*/";
		html += "			table{";
		html += "				border-collapse: collapse;";
		html += "			}";
		html += "		</style>";
		html += "	</head>";
		html += "	<body>";
		html += "		<center><h3>所有联系人</h3></center>";
		html += "		<table align='center' border='1'>";
		html += "			<tr>";
		html += "				<th>编号</th>";
		html += "				<th>姓名</th>";
		html += "				<th>性别</th>";
		html += "				<th>年龄</th>";
		html += "				<th>电话</th>";
		html += "				<th>邮箱</th>";
		html += "				<th>QQ</th>";
		html += "				<th>操作</th>";
		html += "			</tr>";
		if (list != null) {
			for (Contact contact : list) {
				html += "			<tr>";
				html += "				<td>"+contact.getId() +"</td>";
				html += "				<td>"+contact.getName() +"</td>";
				html += "				<td>"+contact.getGender() +"</td>"; 
				html += "				<td>"+contact.getAge() +"</td>";
				html += "				<td>"+contact.getPhone() +"</td>";
				html += "				<td>"+contact.getEmail() +"</td>";
				html += "				<td>"+contact.getQq() +"</td>";
				html += "				<td><a href='/day12Contact/QueryContact?" + contact.getId() + "'>修改</a>";
				html += "		&nbsp;&nbsp;<a href='/day12Contact/DeleteContact?" + contact.getId() + "'>删除</a></td>";
				html += "			</tr>";
			}
		}
		html += "			<tr>";
		html += "				<td colspan='8'><a href='addContact.html'>[添加联系人]</td>";
		html += "			</tr>";
		html += "		</table>";
		html += "		";
		html += "	</body>";
		html += "</html>";
		response.getWriter().write(html);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
